package com.douqu.game.auth.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONPath;
import com.douqu.game.auth.database.mapper.TDMapper;
import com.douqu.game.auth.database.model.*;
import com.douqu.game.auth.utils.TalkingDataUtils;
import com.douqu.game.core.util.CoreUtils;
import com.douqu.game.core.util.LogUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class TDServiceImpl implements TDService {

    @Autowired
    private TDMapper tdMapper;

    @Override
    public void avtivation(TDActivationModel model) {
        try {
            tdMapper.insertAvtivation(JSONObject.toJSONString(model));
        } catch (Exception e) {
            e.printStackTrace();
            LogUtils.errorException(e);
        }
    }

    @Override
    public void register(String account, TDRegisterModel model) {
        int count = tdMapper.selectTDAccountByAccountCount(account);
        try {
            if (count == 0)
                tdMapper.insertTDAccount(account,JSONObject.toJSONString(model),null,null);
            else
                tdMapper.updateTDAccount(account,JSONObject.toJSONString(model),null,null,0);
        }catch (Exception e){
            e.printStackTrace();
            LogUtils.errorException(e);
        }
    }

    @Override
    public void login(String account, TDLoginModel model) {
        int count = tdMapper.selectTDAccountByAccountCount(account);
        try {
            if (count == 0)
                tdMapper.insertTDAccount(account,null,null,JSONObject.toJSONString(model));
            else
                tdMapper.updateTDAccount(account,null,null,JSONObject.toJSONString(model),1);
        }catch (Exception e){
            e.printStackTrace();
            LogUtils.errorException(e);
        }
    }

    @Override
    public void pay(TDPayModel model) {
        try {
            tdMapper.insertTDPay(JSONObject.toJSONString(model));
        } catch (Exception e) {
            e.printStackTrace();
            LogUtils.errorException(e);
        }
    }

    @Override
    public void adClick(TDAdClickModel model) {
        try {
            tdMapper.insertTDAdClick(JSONObject.toJSONString(model));
        }catch (Exception e){
            e.printStackTrace();
            LogUtils.errorException(e);
        }
    }

    @Override
    public void createRole(String account,TDCreateRoleModel model) {
        try {
            int count = tdMapper.selectTDAccountByAccountCount(account);
            if (count == 0)
                tdMapper.insertTDAccount(account,null, JSONObject.toJSONString(model),null);
            else
                tdMapper.updateTDAccount(account,null, JSONObject.toJSONString(model),null,0);
        }catch (Exception e){
            e.printStackTrace();
            LogUtils.errorException(e);
        }
    }
}
